/************
 ** Layui模块化
 ** 全局弹窗和表单模块
 *************/

var layer;
var form;
var laydate;

layui.use(['laydate','layer','form'], function() {
  layer = layui.layer; //弹层
  form = layui.form; //form表单
  laydate = layui.laydate;//日期

  //设置所有日期
  setLaydate('#setuptime');
  setLaydate('#birthdayAdd');
  setLaydate('#intoDateAdd');
  setLaydate('#beginCreateDate');
  setLaydate('#endCreateDate');
  setLaydate('#orderTimeUpe');
  setLaydate('#orderOkTimeUpe');
  setLaydate('#orderEndtimeUpe');
  setLaydate('#beginOrderTime');
  setLaydate('#endOrderTime');

  setLaydate('#orderTimeAdd');
  setLaydate('#orderPayTimeAdd');
  setLaydate('#orderSentTimeUpe');
  setLaydate('#orderReceiptTimeAdd');
  setLaydate('#orderEvalTimeAdd');

  setLaydate('#date1');
  setLaydate('#date2');

  //添加日期功能
  function setLaydate(select){
    if($(select).length != 0){
      laydate.render({elem: select});
    }
  }
});

//只能输入正整数
function number(obj)

{
  obj.value = obj.value.replace(/[^\d+$]/g,"");
}


//只能输入数字
function numMust(obj)

{
  //先把非数字的都替换掉，除了数字和.
  obj.value = obj.value.replace(/[^\d.]/g,"");
}


//只能输入数字和小数点
function numOne(obj)

{
  //先把非数字的都替换掉，除了数字和.
  obj.value = obj.value.replace(/[^\d.]/g,"");
  //必须保证第一个为数字而不是.
  obj.value = obj.value.replace(/^\./g,"");
  //保证只有出现一个.而没有多个.
  obj.value = obj.value.replace(/\.{2,}/g,".");
  //保证.只出现一次，而不能出现两次以上
  obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");

  obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d).*$/, '$1$2.$3'); //只能输入两个小数
}

//只能输入0-5之间的小数,限制一位小数点
function numFive(obj){
  if(!(/^[0-9]+(.[0-9]{0,2})?$/.test(obj.value))) {
    obj.value="";
  }
  if(obj.value > 5 ){
    obj.value = "5";
  }
  if(obj.value < 0){
    obj.value = "0";
  }
}

//弹窗
function openLayer (title, content , ctSize) {

  var size;

  if(ctSize == undefined || ctSize == "" || ctSize == null) {
    size = ['850px', '600px'];
  }else {
    size = ctSize;
  }

  layer.open({
    type: 1,
    title: title,
    shade: [0],
    area: ['85%','85%'],
    maxmin: true,
    content: $(content)
  })
}

// 刷新
function refresh() {
  window.location.reload();
}

/* 重置 */
function reset(tableId) {
  tableId[0].reset();
};

//提示弹窗
function showMsg(msg, icon, title) {

  var titles;

  if(title == undefined || title == "" || title == null) {
    titles = "系统提示";
  }else {
    titles = title;
  }

  layer.alert(msg, {
    skin: 'layui-layer-molv',
    icon: icon,
    title: titles
  });
}

/* 全选/取消全选 */
var selAll = function (tableId , obj) {
  tableId.find('input:checkbox').each(function() {
    $(this).prop('checked', obj.checked);
  });
};


/* 删除多条记录 */
var delBySel = function(obj, url) {

  //获取多选框的value值
  var ids = "";
  obj.find('input[data-ck="ids"]:checked').each(function () {
    ids += ids == "" ? $(this).val() : "," + $(this).val();
  });

  if(ids.length < 1 || ids == "" || ids == null || ids == undefined) {
    layer.msg('你还没有选择任何内容！', {icon: 5});
    return;
  }

  layer.msg('你确定删除当前选中的数据吗？', {
    time: 0 //不自动关闭

    ,btn: ['确定', '取消']

    ,yes: function(){

      //href.location = url + "?ids :" + ids;
      // 删除数据成功
      layer.msg('删除数据成功.', {icon: 6});

      // 删除数据失败
      //layer.msg('删除数据失败.', {icon: 5});

      //刷新页面
      refresh();
    }
  });
};


/* 删除一条记录 */
var delById = function (id, url) {
  if(id == "" || id == null || id == undefined) {
    layer.msg('删除失败,无效的ID.', {icon: 5});
    return;
  }

  layer.msg('你确定删除当前的数据吗？', {
    time: 0 //不自动关闭

    ,btn: ['确定', '取消']

    ,yes: function(){

      //href.location = url + "?id :" + id;

      // 删除数据成功
      layer.msg('删除数据成功.', {icon: 6});

      //删除数据失败
      //layer.msg('删除数据失败.', {icon: 5});

    }
  });

};

//查询表单
function searchSubmit(){
  $('#searchForm').submit();
}



$(function(){

  //菜单栏响应屏幕宽度

  var $shrink = $("#shrink");
  var $side = $(".layui-side");
  var $body = $(".layui-body");
  var $footer = $(".layui-layout-admin .layui-footer");
  var $left = $(".layui-layout-left");
  var $logo = $(".layui-logo");
  var $progress = $(".layui-progress");

  $shrink.click(function(e){
    e.preventDefault();

    if( $shrink.attr("data-side") == "show" ){
      hideSide ()
    }else {
      showSide ()
    }

  });

  //监听屏幕大小变化
  $(window).resize(function () {

    var width = $(window).width();

    if(width < 768){
      hideSide ();
    }else{
      showSide ();
    }

  });

  //隐藏菜单栏
  function hideSide (){
    $body.css("left","0");
    $footer.css("left","0");
    $left.css("left","0");
    $progress.css("left","0");

    $logo.addClass("hide");
    $logo.removeClass("show");
    $side.addClass("hide");
    $side.removeClass("show");

    $shrink.attr("data-side", "hide");
  }

  //展开菜单栏
  function showSide (){
    $body.css("left","200px");
    $footer.css("left","200px");
    $left.css("left","200px");
    $progress.css("left","200px");

    $logo.addClass("show");
    $logo.removeClass("hide");
    $side.addClass("show");
    $side.removeClass("hide");

    $shrink.attr("data-side", "show");
  }

});

//菊花加载
//function load(ele){
//  if($('.shadowDiv').length != 0){
//    $('.shadowDiv').remove();
//  }
//
//  var $ele = ele;
//  if(ele == undefined){
//    $ele = $(".layui-body");
//  }
//
//  var eleWidth = $ele.outerWidth(true);
//  var eleHeight = $ele.outerHeight(true);
//
//  var load = '<div class="shadowDiv" style="position: absolute;left:0;top:0;width:' + eleWidth + 'px ;height:' + eleHeight + 'px;background: rgba(0,0,0,.1);z-index: 19891099;"> <i class="layui-icon layui-anim layui-anim-rotate layui-anim-loop" style="font-size: 40px; color: #1E9FFF;margin: -20px 0; position: absolute; left: 50%; top: 50%;">&#xe63d;</i> </div>';
//
//  $ele.append(load);
//}
//load($('.ibox-content'))

function load(){
  var shadowId = layer.load(1, {
    shade: [0.4,'#fff'] //0.1透明度的白色背景
  });
}

function closeLoad(){
  layer.close(shadowId);
}